﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SlotDuino.UI.Controls
{
    /// <summary>
    /// Interaction logic for HeatRaceResult.xaml
    /// </summary>
    public partial class HeatRaceResult : UserControl
    {
        private DispatcherTimer _timer;
        private TimeSpan _interval;

        #region Dependency properties
        public static readonly DependencyProperty RemainingTimeProperty = DependencyProperty.Register("RemainingTime", typeof(TimeSpan), typeof(HeatRaceResult));
        #endregion

        #region Public properties
        public TimeSpan RemainingTime
        {
            get { return (TimeSpan)GetValue(RemainingTimeProperty); }
            set { SetValue(RemainingTimeProperty, value); }
        }
        #endregion

        public HeatRaceResult()
        {
            InitializeComponent();            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (null == RemainingTime)
                RemainingTime = new TimeSpan(0, 0, 30);

            _interval = new TimeSpan(0, 0, 1);
            
            _timer = new DispatcherTimer();
            _timer.Interval = _interval;
            _timer.Tick += new EventHandler(OnTimerTick);
            _timer.Start();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            if (RemainingTime.Equals(TimeSpan.Zero))
            {
                _timer.Stop();
            }
            else
            {
                RemainingTime = RemainingTime.Subtract(_interval);
            }
        }
    }
}
